package byggeTegner;

import context.*;


import interactor.Interactor;

import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.AbstractButton;
import javax.swing.border.EmptyBorder;

import util.Util2;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

// ***************************************************************************
// *** CLASS: ControlPanel
// ***************************************************************************
public class ControlPanel  extends JPanel implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="mainFrame.ControlPanel";
  static final long serialVersionUID=0; // AD'HOC

  private MouseModePanel mouseModePanel;
  private OptionsPanel optionsPanel;
  
  public ControlPanel()
  { setBorder(new EmptyBorder(5,5,5,5));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    add(mouseModePanel=new MouseModePanel("Mouse modes"));
    add(Box.createRigidArea(new Dimension(10,1)));
    add(optionsPanel=new OptionsPanel("Options"));
    add(Box.createRigidArea(new Dimension(10,1)));
  }
  
  public void setMouseMode(int mouseMode)
  { mouseModePanel.setMouseMode(mouseMode); }

  public void setOptions(boolean showImage,boolean showMarks,boolean showGrid)
  { optionsPanel.setOptions(showImage,showMarks,showGrid); }


  // ***************************************************************************
  // *** LOCAL CLASS: ButtonPanel
  // ***************************************************************************
  class ButtonPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    
    // *************************************************************************
    // *** CONSTRUCTOR: ButtonPanel
    // *************************************************************************
    public ButtonPanel(String title)//,ControlPanel.Listener listener)
    { super();
      setBorder(Util2.getTitleBorder(title));
    }
  
//    protected JButton createButton(String id,ActionListener action,String tip)
//    { JButton btn=new JButton(id);
////    btn.setMaximumSize(new Dimension(width,height));
////    btn.setMinimumSize(new Dimension(width,height));
////    btn.setPreferredSize(new Dimension(width,height));
//      if(action==null) btn.setEnabled(false);
//      else btn.addActionListener(action);
//      btn.setToolTipText(tip);
//      btn.setMaximumSize(new Dimension(100,20));
//      return(btn);
//    }  
  
    protected JRadioButton createRadioButton
    (String id,ButtonGroup group,boolean selected,String tip,ActionListener action)
    { JRadioButton btn=new JRadioButton(id,selected);
      if(action==null) btn.setEnabled(false);
      else btn.addActionListener(action);
      if(group!=null) group.add(btn);
      btn.setToolTipText(tip);
      btn.setMaximumSize(new Dimension(100,20));
      return(btn);
    }  
  }


  // ***************************************************************************
  // *** LOCAL CLASS: OptionsPanel
  // ***************************************************************************
  class OptionsPanel extends ButtonPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JRadioButton showObjectsBtn,showImageBtn,showGridBtn;

    public void note(String msg)
    { JOptionPane.showMessageDialog(null,msg); }
    
    // *************************************************************************
    // *** CONSTRUCTOR: OptionsPanel
    // *************************************************************************
    public OptionsPanel(String title)//,ControlPanel.Listener listener)
    { super(title);//,listener);
      setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//    setAlignmentX(LEFT_ALIGNMENT);
//      setAlignmentX(CENTER_ALIGNMENT);
//      setAlignmentY(TOP_ALIGNMENT);

      JPanel panel=new JPanel();
      add(panel);
      panel.setLayout(new GridLayout(3,1));
//    ButtonGroup group = new ButtonGroup();
         
      panel.add(showObjectsBtn=createRadioButton("Show Objects",null,true
    	,"Click to switch object drawing on/off",new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { AbstractButton btn=(AbstractButton)e.getSource();
//          MapEditor.getSelectedMapViewer().showObjectsAction(btn.isSelected());
          DrawPanel currentDrawPanel=ByggeTegner.getCurrentDrawPanel();
          if(currentDrawPanel!=null) currentDrawPanel.showObjectsAction(btn.isSelected());
        }
      }));
      panel.add(showImageBtn=createRadioButton("Show Map",null,true
        ,"Click to switch map image drawing on/off",new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { AbstractButton btn=(AbstractButton)e.getSource();
//          MapEditor.getSelectedMapViewer().setShowMapImage(btn.isSelected());
          DrawPanel currentDrawPanel=ByggeTegner.getCurrentDrawPanel();
          if(currentDrawPanel!=null) currentDrawPanel.setShowMapImage(btn.isSelected());
        }
      }));
      panel.add(showGridBtn=createRadioButton("Show Grid",null,true
        ,"Click to switch UTM Grid drawing on/off",new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { AbstractButton btn=(AbstractButton)e.getSource();
          DrawPanel currentDrawPanel=ByggeTegner.getCurrentDrawPanel();
          if(currentDrawPanel!=null) currentDrawPanel.setShowGrid(btn.isSelected());
        }
      }));
    }

    public void setOptions(boolean showImage,boolean showObjects,boolean showGrid)
    { showImageBtn.setSelected(showImage);
      showObjectsBtn.setSelected(showObjects);
      showGridBtn.setSelected(showGrid);
    }

  }


  // ***************************************************************************
  // *** LOCAL CLASS: MouseModePanel
  // ***************************************************************************
  class MouseModePanel extends ButtonPanel
  { static final long serialVersionUID=0; // AD'HOC
	private JRadioButton measureBtn,editBtn
	       ,drawLineBtn,drawPolygonBtn,drawEllipseBtn,drawParallelogramBtn,drawPathBtn,drawTextBtn;
      
    // *************************************************************************
    // *** CONSTRUCTOR: MouseModePanel
    // *************************************************************************
    public MouseModePanel(String title)
    { super(title);
      setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//    setAlignmentX(LEFT_ALIGNMENT);
//      setAlignmentX(CENTER_ALIGNMENT);
//      setAlignmentY(TOP_ALIGNMENT);
//      setAlignmentY(CENTER_ALIGNMENT);

      JPanel panel=new JPanel();
      panel.setLayout(new GridLayout(2,1));
      ButtonGroup group=new ButtonGroup();
      panel.add(getUpperPanel(group));
      panel.add(getDrawPanel(group));
      add(panel);
    }
    
    private ActionListener action(final int mouseMode)
    { return(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        {
//    	  MapEditor.getSelectedMapViewer().setMouseAdapter(mouseMode);
          DrawPanel currentDrawPanel=ByggeTegner.getCurrentDrawPanel();
          if(currentDrawPanel!=null) currentDrawPanel.setInteractorAdapter(mouseMode);
    	}
      });
    }
    private JPanel getUpperPanel(ButtonGroup group)
    { JPanel panel=new JPanel();
      panel.setLayout(new GridLayout(2,1));
      panel.add(measureBtn=createRadioButton("Measure",group,true,"Click to set Measure mode",action(Interactor.MEASURE)));
      panel.add(editBtn=createRadioButton("Edit",group,false,"Click to set editing mode",action(Interactor.EDIT)));
      return(panel);	
    }
    
    private JPanel getDrawPanel(ButtonGroup group)
    { JPanel panel=new JPanel();
      panel.setBorder(Util2.getTitleBorder("Draw"));
      panel.setLayout(new GridLayout(6,1));
      panel.add(drawLineBtn=createRadioButton("Line",group,false,"Click to set Draw Line mode",action(Interactor.DRAW_LINE)));
      panel.add(drawPolygonBtn=createRadioButton("Polygon",group,false,"Click to set Draw Polygon mode",action(Interactor.DRAW_POLYGON)));
      panel.add(drawEllipseBtn=createRadioButton("Ellipse",group,false,"Click to set Draw Ellipse mode",action(Interactor.DRAW_ELLIPSE)));
      panel.add(drawParallelogramBtn=createRadioButton("Parallelogram",group,false,"Click to set Draw Parallelogram mode",action(Interactor.DRAW_PARALLELOGRAM)));
      panel.add(drawPathBtn=createRadioButton("Path",group,false,"Click to set Draw Path mode",action(Interactor.DRAW_PATH)));
      panel.add(drawTextBtn=createRadioButton("Text",group,false,"Click to set Draw Text mode",action(Interactor.DRAW_TEXT)));
      return(panel);	
    }
  
    public void setMouseMode(int mouseMode)
    {  switch(mouseMode)
      { case Interactor.MEASURE:        measureBtn.setSelected(true);       break;
        case Interactor.EDIT:           editBtn.setSelected(true);          break; 
        case Interactor.DRAW_LINE:      drawLineBtn.setSelected(true);      break;
        case Interactor.DRAW_POLYGON:   drawPolygonBtn.setSelected(true);   break;
        case Interactor.DRAW_ELLIPSE:   drawEllipseBtn.setSelected(true);   break;
        case Interactor.DRAW_PARALLELOGRAM: drawParallelogramBtn.setSelected(true); break;
        case Interactor.DRAW_PATH:      drawPathBtn.setSelected(true);      break;
        case Interactor.DRAW_TEXT:      drawTextBtn.setSelected(true);      break;
      }  
    }

  }
}
    